package org.shilong.UI.businessCard;

import java.util.LinkedList;
import java.util.List;

import org.shilong.MContext.Json.JSonContext;
import org.shilong.MContext.WebRequest.WebRequestContext;
import org.shilong.Model.BusinessCardModel;
import org.shilong.UI.R;
import org.shilong.Utility.IntentUtility;
import org.shilong.Utility.ProgressDialogUtility;
import org.shilong.Utility.SharedPreferenceUtility;
import org.shilong.Utility.ToastUtility;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ShowBusinessCardFrame extends Activity implements OnClickListener {

	private Button to_alert_button;

	private TextView show_tele_textview, show_com_name_textview,
	show_business_textivew, show_domain_textview, show_sex_textview,
	show_age_textview, refresh_business_card_textview;

	private ImageView refresh_business_card_imageview;

	/* 显示名片list */
	public static LinkedList<BusinessCardModel> showBusinessCardList = new LinkedList<BusinessCardModel>();

	private Animation refreshAnim;

	private String[] arr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.layout_show_business_card_content);

		refreshAnim = AnimationUtils.loadAnimation( this, R.anim.refresh_rotate_anim );

		arr = SharedPreferenceUtility
				.getPersonInfoFromSharedPreference(ShowBusinessCardFrame.this);

		initViews();

		registerEvents();

		// handler.post( new MyJob() );
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub

		super.onResume();
	}


	private void initViews() {

		show_tele_textview = (TextView) this
				.findViewById(R.id.show_tele_num_textview);
		show_com_name_textview = (TextView) this
				.findViewById(R.id.show_com_name_textview);
		show_business_textivew = (TextView) this
				.findViewById(R.id.show_business_textview);
		show_domain_textview = (TextView) this
				.findViewById(R.id.show_domain_textview);
		show_sex_textview = (TextView) this
				.findViewById(R.id.show_sex_textview);
		show_age_textview = (TextView) this
				.findViewById(R.id.show_age_textview);

		refresh_business_card_textview = (TextView) this
				.findViewById(R.id.refresh_business_card_textview);

		to_alert_button = (Button) this
				.findViewById(R.id.to_my_alert_frame_business_button);

		refresh_business_card_imageview = (ImageView)this.findViewById( R.id.refresh_business_card_imageview );
		
		initDatas(showBusinessCardList);

	}

	private void registerEvents() {

		to_alert_button.setOnClickListener(this);

		refresh_business_card_textview.setOnClickListener(this);

	}

	/**
	 * 为textivew初始化数据
	 * 
	 * @param list
	 */
	private void initDatas(LinkedList<BusinessCardModel> list) {

		BusinessCardModel model = list.get(0);

		show_tele_textview.setText(Html.fromHtml("<u>"
				+ model.getTelephone_number() + "</u>"));
		show_com_name_textview.setText(Html.fromHtml("<u>"
				+ model.getCompany_name() + "</u>"));
		show_business_textivew.setText(Html.fromHtml("<u>"
				+ model.getBussiness() + "</u>"));
		show_domain_textview.setText(Html.fromHtml("<u>" + model.getDomain()
				+ "</u>"));
		show_age_textview
		.setText(Html.fromHtml("<u>" + model.getAge() + "</u>"));
		show_sex_textview
		.setText(Html.fromHtml("<u>" + model.getSex() + "</u>"));

	}

	/**
	 * 刷新名片
	 * 
	 * @author cesc
	 * 
	 */
	private class QueryBusinessCardTask extends
	AsyncTask<String, String, String> {

		private Context mContext;

		public QueryBusinessCardTask(Context con) {

			this.mContext = con;

		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub

			// initDatas( mList );

			LinkedList<BusinessCardModel> llTwo = new LinkedList<BusinessCardModel>();

			llTwo = (LinkedList<BusinessCardModel>) JSonContext
					.createInstance(JSonContext.TYPE_GET_BUSINESS_CARD)
					.getJsonRlt( result );

			ShowBusinessCardFrame.showBusinessCardList = llTwo;

			initDatas(showBusinessCardList);

			refresh_business_card_imageview.clearAnimation();
			ToastUtility.showToast( ShowBusinessCardFrame.this, "刷新成功.." );
			System.out.println("获取成功新的名片信息..");
			

		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			/*ProgressDialogUtility.showDialog(mContext, "请求中..");*/
			
			refresh_business_card_imageview.startAnimation( refreshAnim );
		}

		@SuppressWarnings("unchecked")
		@Override
		protected String doInBackground(String... params) {
			
			System.out.println("开始获取新的名片信息..");
			/* 获取名片信息 */
			String rlt = "nodatas";
			
			rlt = WebRequestContext.createInstance(
					WebRequestContext.TYPE_QUERY_BUSINESS_CARD)
					.getWebRequestRlt(arr[2]);

			return rlt;
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.to_my_alert_frame_business_button:

			to_alert_logic();

			break;
		case R.id.refresh_business_card_textview:

			to_refresh_logic();

			break;
		default:
			break;
		}

	}

	private void to_alert_logic() {

		BusinessCardModel model = showBusinessCardList.get(0);
		Bundle bundle = new Bundle();

		bundle.putString("telephonenum", model.getTelephone_number());
		bundle.putString("com_name", model.getCompany_name());
		bundle.putString("buss", model.getBussiness());
		bundle.putString("domain", model.getDomain());
		bundle.putString("sex", model.getSex());
		bundle.putString("age", model.getAge());

		IntentUtility.goNextFrame(ShowBusinessCardFrame.this,
				AlertBusinessCardFrame.class, bundle);
	}

	/**
	 * 刷新逻辑
	 */
	private void to_refresh_logic(){

		System.out.println( "刷新");
		
		QueryBusinessCardTask task = new QueryBusinessCardTask( this  );
		
		task.execute();
		
		

		//refresh_business_card_textview.setAnimation(animation)


	}
}
